#include "comm.hpp"

int main()
{
    key_t key = ftok(PATH_NAME, PROJ_ID);
    if(key < 0)
    {
        perror("ftok");
        return 1;
    }

    int shmid = shmget(key, SIZE, IPC_CREAT|IPC_EXCL|0600);//创建全新的共享内存，如果冲突，我们出错返回
    if(shmid < 0)
    {
        perror("shmget");
        return 2;
    }
    cout << "key:" << key << "shmid:" << shmid << endl;
    
    char *mem = (char*) shmat(shmid, NULL, 0);
    cout << "attaches shm success" << endl;
    //在共享内存中读取数据
    int cnt = 26;
    while(cnt--)
    {
        cout << mem << endl;
        //sleep(1);
    }
    shmdt(mem);
    cout << "process detaches success" << endl;
    //sleep(5);
    shmctl(shmid, IPC_RMID, NULL); 
    cout << "key:" << key << " shmid:" << shmid << "->shm delete sucess..." << endl;
    //sleep(5);
    return 0;
}